package client;

import java.io.EOFException;
import java.io.IOException;
import java.net.SocketException;
import shared.*;

public class ReceivePacketThread extends Thread{
    TestingClient host;

    public ReceivePacketThread(TestingClient host){
        this.host = host;
    }

    public void run(){
        host.clientGUI.addLine("Receive thread started.");
        while (true){
            try {
                Packet packet = (Packet)host.in.readObject();
                host.packetHandler.handlePacket(packet);
            } catch (SocketException e) {
                host.isConnected = false;
                host.clientGUI.addLine("Connection lost to server.");
                host.clientGUI.addLine("Shutting down Receive Thread.");
                return;
            } catch (EOFException e) {
                host.isConnected = false;
                host.clientGUI.addLine("Connection lost to server.");
                host.clientGUI.addLine("Shutting down Receive Thread.");
                return;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
